<?php 
	$user_id = 0;

	//for form submit
	if($_SERVER['REQUEST_METHOD'] == "POST"){

		//process POST var
		$user_post 	= array(
    					'username' 			=> $_POST['username'],
    					'password' 			=> md5($_POST['password']),
    					'user_type_id' 		=> $_POST['user_type_id'],
    					'is_practicioner' 	=> $_POST['is_practicioner'],
    					'active' 			=> $_POST['active']
					);

		$user_group_post = array(
						'user_id'  => $_POST['user_id'],
    					'group_id' => $_POST['group_id']
					);

		$user_practicioner 	= array(
						'user_id'  		=> $_POST['user_id'],
						'name' 			=> $_POST['name'],
					    'address' 		=> $_POST['address'], 
					    'city' 			=> $_POST['city'],
					    'zipcode' 		=> $_POST['zipcode'],
					    'phone' 		=> $_POST['phone'],
					    'fax' 			=> $_POST['fax'],
					    'presciber_no' 	=> $_POST['presciber_no'],
					    'provider_no' 	=> $_POST['provider_no'],
					    'hpi_i_no' 		=> $_POST['hpi_i_no'],
					    'state_reg_no' 	=> $_POST['state_reg_no'],
					    'qualification' => $_POST['qualification'],
					    'email' 		=> $_POST['email'],
					    'pcehr_title' 	=> $_POST['pcehr_title'],
					    'pcehr_fname' 	=> $_POST['pcehr_fname'],
					    'pcehr_mname' 	=> $_POST['pcehr_mname'],
					    'pcehr_lname' 	=> $_POST['pcehr_lname'],
					    'pcehr_check' 	=> $_POST['pcehr_check'],
					    'eprescibing_status' 			=> $_POST['eprescibing_status'],
					    'eprescribing_disable' 			=> $_POST['eprescribing_disable'],
					    'eprescribing_enable_ask' 		=> $_POST['eprescribing_enable_ask'],
					    'eprescribing_enable_dont_ask' 	=> $_POST['eprescribing_enable_dont_ask'],
					    'top_level_access' 				=> $_POST['top_level_access'],
					    'pki_encryption' 				=> $_POST['pki_encryption'],
					    'data_export' 					=> $_POST['data_export'],
					    'auto_capitalizing' 			=> $_POST['auto_capitalizing'],
					    'option_edition' 				=> $_POST['option_edition'],
					    'status' 						=> $_POST['status']
					);
	
		$user_non_practicioner 	= array(
						'user_id'  				=> $_POST['user_id'],
						'name' 					=> $_POST['name'],
					    'ancillary_provider' 	=> $_POST['ancillary_provider'],
					    'hpi_i_no' 				=> $_POST['hpi_i_no'],
					    'pcehr_title' 			=> $_POST['pcehr_title'],
					    'pcehr_fname' 			=> $_POST['pcehr_fname'],
					    'pcehr_mname' 			=> $_POST['pcehr_mname'],
					    'pcehr_lname' 			=> $_POST['pcehr_lname'],
					    'auto_capitalize' 		=> $_POST['auto_capitalize'],
					    'supervisor_id' 		=> $_POST['supervisor_id']
					);

		if(isset($_POST['id']) && !empty($_POST['id'])){
			//edit user
			$user_def 	= array(
							'update' => $user_post,
							'filter' => 'id = '.$_POST['id']
						  );
			$user 		= new User($user_def);
			$user->Update();

			//edit user_group
			$group_filter 	= 'user_id = '.$_POST['id'];
			$params 		= array('filter' => $group_filter);
			$obj 			= new UserGroup($params);
			$exists 		= $obj->Count();

			if(!empty($exists) && $exists > 0){
				$group_def 	= array(
								'update' => $user_group_post,
								'filter' => $group_filter
							  );
				$user_group = new UserGroup($group_def);
				$user_group->Update();
			}else{
				$group_def 		= array('add' => $user_group_post);
				$user_group 	= new UserGroup($group_def);
				$user_group_id 	= $user_group->Insert();
			}

			//edit practicioner or non practicioner
			if($_POST['is_practicioner'] == 1){
				$filter = 'user_id = '.$_POST['id'];
				$params = array('filter' => $filter);
				$obj 	= new UserPracticioner($params);
				$exists = $obj->Count();

				if(!empty($exists) && $exists > 0){
					$prac_def 	= array(
									'update' => $user_practicioner,
									'filter' => $filter
								  );
					$user_prac 	= new UserPracticioner($prac_def);
					$user_prac->Update();
				}else{
					$prac_def 	= array('add' => $user_practicioner);
					$user_prac 	= new UserPracticioner($prac_def);
					$user_prac->Insert();
				}
			}else{
				$filter = 'user_id = '.$_POST['id'];
				$params = array('filter' => $filter);
				$obj 	= new UserNonPracticioner($params);
				$exists = $obj->Count();

				if(!empty($exists) && $exists > 0){
					$prac_def 	= array(
									'update' => $user_non_practicioner,
									'filter' => $filter
							  	);
					$user_prac 	= new UserNonPracticioner($prac_def);
					$user_prac->Update();
				}else{
					$prac_def 	= array('add' => $user_non_practicioner);
					$user_prac 	= new UserNonPracticioner($prac_def);
					$user_prac->Insert();
				}
			}
			$message = 2;

		}else{

			//add to user
			$user_post['date_created'] = date("Y-m-d h:i:s"); 
			$user_def 	= array('add' => $user_post);
			$user 		= new User($user_def);
			$user_id 	= $user->Insert();
			
			//add to user_group
			if(!empty($_POST['group_id'])){
				$user_group_post['user_id'] = $user_id;
				$group_def 		= array('add' => $user_group_post);
				$user_group 	= new UserGroup($group_def);
				$user_group_id 	= $user_group->Insert();
			}

			//add to practicioner or non practicioner
			if(!empty($_POST['name'])){
				if($_POST['is_practicioner'] == 1){
					$user_practicioner['user_id'] = $user_id;
					$prac_def 	= array('add' => $user_practicioner);
					$user_prac 	= new UserPracticioner($prac_def);
					$user_prac->Insert();
				}else{
					$user_non_practicioner['user_id'] = $user_id;
					$prac_def 	= array('add' => $user_non_practicioner);
					$user_prac 	= new UserNonPracticioner($prac_def);
					$user_prac->Insert();
				}
			}
			$message = 1;
		}
	}

	//for edit form - fetch data
	if($_SERVER['REQUEST_METHOD'] == "GET"){
		$message = 0;
		if(isset($_GET['id']) && !empty($_GET['id'])){
			$user_id = $_GET['id'];

			//fetch users and user group details
			$params 	= array('filter' => 'user.id = '.$user_id);
			$users 		= new User($params);
			$users->AddJoin("LEFT JOIN", "user_group", "user_group.user_id=".$user_id, "user_group.group_id");
			$user_row 	= $users->Select();

			$disable_password = "";
			if(!empty($user_id)){
				$disable_password = "disabled='disabled'";
			}
		}
	}

	//for select lists
	//dropdown select for user category
	$userCategoryObj = new UserType();
	if($_SESSION['user']['user_type_id'] != CONST_SUPER_ADMIN_TYPE){
		$userCategoryObj->filter = "user_type.id > ".CONST_SUPER_ADMIN_TYPE;
	}
	if(!empty($user_id) && isset($user_row['user_type_id']) && !empty($user_row['user_type_id'])){
		$userCategoryObj->select_list_default = $user_row['user_type_id'];
	}
	$user_type_select = $userCategoryObj->SelectList();

	//dropdown select for group
	$groupObj =  new Group();
	if(!empty($user_id) && isset($user_row['group_id']) && !empty($user_row['group_id'])){
		$groupObj->select_list_default = $user_row['group_id'];
	}
	$group_select = $groupObj->SelectList();
?>